package com.zhmsky.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zhmsky.entity.User;
import com.zhmsky.model.ResultMap;
import com.zhmsky.service.UserService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhmsky
 * @date 2021/12/6 10:11
 */
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private UserService userService;
    @Autowired
    private ResultMap resultMap;

    /**
     * @description 管理员能查询所有用户信息
     * @return
     */
    @GetMapping("/getUser")
    @RequiresRoles("admin")
    public ResultMap getUser(){
        List<User> userList = userService.list();
        return resultMap.success().code(200).message(userList);
    }

    /**
     * @description 模糊查询，按姓氏查询
     * @permission 拥有admin角色的用户才能访问
     * @param name 姓氏
     * @return
     */
    @GetMapping("/getUserByName")
    @RequiresRoles(logical = Logical.OR, value = {"admin"})
    public ResultMap getUserByCondition(String name){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.likeRight("username",name);   //等效于 like 老%
        List<User> userList = userService.list(wrapper);
        return resultMap.success().code(200).message(userList);
    }

    /**
     * @description 按照性别分类查询用户信息
     * @param sex 整型参数 0：男  1：女
     * @return
     */
    @GetMapping("/getUserBySex")
    @RequiresRoles(logical = Logical.OR, value = {"admin"})
    public ResultMap getUserBySex(Integer sex){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("sex",sex);
        List<User> userList = userService.list(wrapper);
        return resultMap.success().code(200).message(userList);
    }

    /**
     * @description 管理员可以封号
     * @param username
     * @return
     */
    @PostMapping("/banUser")
    @RequiresRoles("admin")
    public ResultMap banUser(String username){
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.eq("username",username).set("ban",1);
        boolean b = userService.update(wrapper);
        if(b){
            return resultMap.success().code(200).message("成功将该用户封号！");
        }else{
            return resultMap.fail().code(400).message("操作失败，请重试！");
        }
    }

    /**
     * @description 查询所有被封号账户
     * @return
     */
    @GetMapping("/getBanUser")
    @RequiresRoles("admin")
    public ResultMap getBanUser(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("ban",1);
        List<User> userList = userService.list(wrapper);
        return resultMap.success().code(200).message(userList);
    }

}
